﻿///***************************************************************
/// 시  스  템 : SMFramework
/// 단위시스템 : Util
/// 계      층 : Framework Utility
/// 프로그램명 : DataTableUtil
/// 파  일  명 : DataTableUtil.cs
/// 설      명 : DataTable관련 유틸리티
///***************************************************************
/// 수정ID 일자       이름     내용
///---------------------------------------------------------------
/// CH001  2012-03-23 송시명   최초 작성
///***************************************************************
///
using System;
using System.Data;
using System.Text;

namespace SMFramework.Utility
{
	/// <summary>
	/// DataTable관련 유틸리티
	/// </summary>
	public sealed class DataTableUtil
	{
		private DataTableUtil()
		{
		}

		#region RowSpanDataTable
		/// <summary>
		/// DataTable을 세로로 머지하여 HTML문자열로 리턴. 머지될 열은 (당연히)ORDER BY가 되어있어야 함.
		/// </summary>
		/// <param name="dt">DataTable</param>
		/// <param name="targetIdx">머지할 열의 index값</param>
		/// <returns></returns>
		public static string RowSpanDataTable(DataTable dt, int targetIdx)
		{
			StringBuilder sb = new StringBuilder();

			string sourceStr = "";
			string targetStr = "";
			string columnName = dt.Columns[targetIdx].ColumnName;
			int rowspan = 0;

			sb.Append("<table border=\"1\">\n");

			//행
			for (int i = 0; i < dt.Rows.Count; i++)
			{
				sourceStr = dt.Rows[i][targetIdx].ToString();	//비교할 문자열

				sb.Append("<tr>\n");

				//열
				for (int j = 0; j < dt.Columns.Count; j++)
				{
					string data = dt.Rows[i][j].ToString();

					//지정된 열만 머지
					if (j == targetIdx)
					{
						if (sourceStr != targetStr)
						{
							//같은 데이터 수 만큼
							rowspan = dt.Select(columnName + " = '" + sourceStr + "'").Length;

							sb.Append("<td rowspan=\"" + rowspan.ToString() + "\">" + data + "</td>");
							targetStr = sourceStr;
						}
					}
					else
					{
						sb.Append("<td>" + data + "</td>");
					}
				}
				sb.Append("\n</tr>\n");
			}
			sb.Append("</table>");

			return sb.ToString();
		}

		#endregion

		#region GetFldStr
		/// <summary>
		/// DataTable에서 인자, 필드로 0번째 string값 가져옴
		/// </summary>
		/// <param name="dt"></param>
		/// <param name="fieldname"></param>
		/// <returns></returns>
		public static string GetFldStr(DataTable dt, string fieldname)
		{
			return GetFldStr(dt, 0, fieldname);
		}

		/// <summary>
		/// DataTable에서 인자, 필드로 string값 가져옴
		/// </summary>
		/// <param name="dt"></param>
		/// <param name="idx"></param>
		/// <param name="fieldname"></param>
		/// <returns></returns>
		public static string GetFldStr(DataTable dt, int idx, string fieldname)
		{
			if (dt.Columns.Contains(fieldname))
			{
				return dt.Rows[idx][fieldname].ToString().Trim();
			}
			else
			{
				return "";
			}
		}
		#endregion

		#region GetFldInt
		/// <summary>
		/// DataTable에서 필드로 0번째 int 값 가져옴
		/// </summary>
		/// <param name="dt"></param>
		/// <param name="fieldname"></param>
		/// <returns></returns>
		public static int GetFldInt(DataTable dt, string fieldname)
		{
			return GetFldInt(dt, 0, fieldname);
		}

		/// <summary>
		/// DataTable에서 인자, 필드로 int 값 가져옴
		/// </summary>
		/// <param name="dt"></param>
		/// <param name="idx"></param>
		/// <param name="fieldname"></param>
		/// <returns></returns>
		public static int GetFldInt(DataTable dt, int idx, string fieldname)
		{
			try
			{
				if (dt.Columns.Contains(fieldname))
				{
					return Convert.ToInt32(dt.Rows[idx][fieldname].ToString().Trim());
				}
				else
				{
					return 0;
				}
			}
			catch (Exception ex)
			{
				throw ex;
			}
		}
		#endregion

		#region GetFldDouble
		/// <summary>
		/// DataTable에서 필드로 0번째 double 값 가져옴
		/// </summary>
		/// <param name="dt"></param>
		/// <param name="fieldname"></param>
		/// <returns></returns>
		public static double GetFldDbl(DataTable dt, string fieldname)
		{
			return GetFldDbl(dt, 0, fieldname);
		}

		/// <summary>
		/// DataTable에서 인자, 필드로 double 값 가져옴
		/// </summary>
		/// <param name="dt"></param>
		/// <param name="idx"></param>
		/// <param name="fieldname"></param>
		/// <returns></returns>
		public static double GetFldDbl(DataTable dt, int idx, string fieldname)
		{
			try
			{
				if (dt.Columns.Contains(fieldname))
				{
					return Convert.ToDouble(dt.Rows[idx][fieldname].ToString().Trim());
				}
				else
				{
					return 0.00;
				}
			}
			catch (Exception ex)
			{
				throw ex;
			}
		}
		#endregion

		#region CheckColumn

		/// <summary>
		/// DataTable에 컬럼이 있는지 체크
		/// </summary>
		/// <param name="dt"></param>
		/// <param name="column_name"></param>
		/// <returns></returns>
		public static bool CheckColumn(DataTable dt, string column_name)
		{
			return dt.Columns.Contains(column_name);
		}

		#endregion

		#region AddColumn

		/// <summary>
		/// DataTable에 컬럼 추가
		/// </summary>
		/// <param name="dt"></param>
		/// <param name="column_name"></param>
		/// <param name="type_name"></param>
		/// <returns></returns>
		public static bool AddColumn(DataTable dt, string column_name, Type type_name)
		{
			if (CheckColumn(dt, column_name))
			{
				return false;
			}
			else
			{
				dt.Columns.Add(column_name, type_name);
				return true;
			}
		}

		#endregion

		#region GetStringFromDataTable
		/// <summary>
		/// 데이터테이블의 특정 필드의 값을 하나의 문자열로 리턴 (구분자는 파이프라인)
		/// </summary>
		/// <param name="po_dt"></param>
		/// <param name="ps_field_name"></param>
		/// <returns></returns>
		public static string GetStringFromDataTable(DataTable po_dt, string ps_field_name)
		{
			return GetStringFromDataTable(po_dt, ps_field_name, "|");
		}

		/// <summary>
		/// 데이터테이블의 특정 필드의 값을 하나의 문자열로 리턴 (지정한 구분자로 구분)
		/// </summary>
		/// <param name="po_dt"></param>
		/// <param name="ps_field_name"></param>
		/// <param name="delimiter"></param>
		/// <returns></returns>
		public static string GetStringFromDataTable(DataTable po_dt, string ps_field_name, string delimiter)
		{
			string ls_data = "";

			if (po_dt.Rows.Count > 0)
			{
				for (int i = 0; i < po_dt.Rows.Count; i++)
				{
					ls_data += DataTableUtil.GetFldStr(po_dt, i, ps_field_name) + delimiter;
				}
			}

			return ls_data.Substring(0, ls_data.LastIndexOf(delimiter));
		}


		#endregion

	}
}
